package com.zsp.comic.service.impl;

import com.zsp.comic.dao.UserDao;
import com.zsp.comic.entity.Authority;
import com.zsp.comic.entity.User;
import com.zsp.comic.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author gan
 * @create 2020-12-03 16:51
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    //密码加密
    @Bean
    private PasswordEncoder cipherPassword(){
        return new BCryptPasswordEncoder();
    }

    @Override
    public User getUserByUsernameAndPassword(String username, String password) {
        User hasUser = getUserByUsername(username);
        boolean isPasswordMatches = cipherPassword().matches(password.trim(), hasUser.getPassword());
        if (null != hasUser && isPasswordMatches){
            return hasUser;
        }
        return null;
    }

    @Override
    public User getUserByUsername(String username) {
        return userDao.getUserByUsername(username) != null ? userDao.getUserByUsername(username) : null;
    }

    @Override
    public String getUserAuthorityById(Integer id) {
        String authority = userDao.getUserAuthorityById(id);
        return authority;
    }

    @Override
    public boolean addUser(String username, String password, Date registerTime) {
        User hasUser = userDao.getUserByUsername(username);
        Integer isAdd = 0;
        Integer isAddAuthority = 0;
        if (null == hasUser){
            isAdd = userDao.addUser(new User(username, cipherPassword().encode(password), registerTime));
            if (isAdd > 0){
                isAddAuthority = userDao.addUserAuthority(userDao.getUserByUsername(username).getId());
                return isAdd > 0 && isAddAuthority > 0;
            }
        }
        return false;
    }

    @Override
    public boolean addUserCollect(String username, Integer comicId) {
        User hasUser = userDao.getUserByUsername(username);
        Integer isExistCollect = userDao.getUserCollectNumByUserIdAndComicId(hasUser.getId(), comicId);
        Integer isCollect = 0;
        if (null != hasUser && isExistCollect < 1){
            isCollect = userDao.addUserCollect(hasUser.getId(), comicId);
            return isCollect > 0;
        }
        return false;
    }

    @Override
    public boolean deleteUserCollect(String username, Integer comicId) {
        User hasUser = userDao.getUserByUsername(username);
        if (null != hasUser && userDao.getUserCollectNumByUserIdAndComicId(hasUser.getId(), comicId) > 0){
            return userDao.deleteUserCollect(userDao.getUserByUsername(username).getId(), comicId) > 0;
        }
        return false;
    }


    @Override
    public boolean updateUser(String username, String password, String newPassword, String confirmNewPassword) {
        User hasUser = userDao.getUserByUsername(username);
        boolean isPasswordMatches = cipherPassword().matches(password.trim(), hasUser.getPassword());
        Integer isUpdate = 0;
        if (null != hasUser && isPasswordMatches && newPassword.equals(confirmNewPassword)){
            isUpdate = userDao.updateUser(new User(username, cipherPassword().encode(newPassword.trim())));
            return isUpdate > 0;
        }
        return false;
    }

}
